package com.snow.auth.service;

import cn.hutool.core.collection.CollUtil;
import com.snow.jpa.BaseService;
import com.snow.auth.entity.SystemDept;
import com.snow.auth.repository.SystemDeptRepository;
import com.snow.util.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * TODO
 *
 * @author by 13119
 */
@Service
public class SystemDeptService extends BaseService<SystemDeptRepository, SystemDept, Integer> {

    public SystemDeptService(SystemDeptRepository repository) {
        super(repository);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<SystemDept> tree() {
        List<SystemDept> deptList = lambdaQuery().findAll();
        if (deptList.isEmpty()) {
            return CollUtil.newArrayList();
        }
        return TreeUtil.buildTree(deptList, 0, SystemDept::getId, SystemDept::getParentId, SystemDept::setChildren, Comparator.comparingInt(SystemDept::getSort));
    }


}
